//
//  LBCategoryViewController.m
//  jslj
//
//  Created by luckyRoy on 2017/10/10.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCategoryViewController.h"
#import "LBNewCategoryMainView.h"
#import <Masonry.h>
#import "UIApplication+LBViewController.h"

@interface LBCategoryViewController ()

@property (nonatomic, strong) LBNewCategoryMainView *categoryView;

@property (nonatomic, copy) LBCategoryViewControllerBlock clickBlock;

@property (nonatomic, assign) LBCategoryBizCodeType codeType;

@property (nonatomic, strong) NSArray *selectedCodeArray;

@property (nonatomic, assign) BOOL isSingleSelected;

@property (nonatomic, assign) NSInteger maxSelectedLevel;

//@property (nonatomic, strong) UIViewController *beginViewController;

@end

@implementation LBCategoryViewController

- (instancetype)initWithType:(LBCategoryBizCodeType)codeType
                 chooseArray:(NSArray <NSString *> *)codeArray
            isSingleSelected:(BOOL)isSingleSelected
            maxSelectedLevel:(NSInteger)level
                    complete:(LBCategoryViewControllerBlock)complete
{
    if (self = [super init]) {
        self.clickBlock = complete;
        self.codeType = codeType;
        self.isSingleSelected = isSingleSelected;
        self.selectedCodeArray = codeArray;
        self.maxSelectedLevel = level;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"分类选择";
    if (self.codeType == LBCategoryBizCodeTypeTender)
    {
        self.title = @"项目分类";
    }
    
    [self.view addSubview:self.categoryView];
    
    [self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

//- (void)setBeginViewController
//{
//    UINavigationController *currentNavViewController = [UIApplication getCurrentViewController].navigationController;
//
//    if (!currentNavViewController) {
//        currentNavViewController = [UIApplication sharedApplication].keyWindow.rootViewController ;
//    }
//
//
//    NSInteger viewCount = currentNavViewController.viewControllers.count;
//
//    NSInteger lastViewControllerIndex = (viewCount - 2);
//
//    if (lastViewControllerIndex >= 0) {
//        self.beginViewController = currentNavViewController.viewControllers[lastViewControllerIndex];
//    }
//}
//
//- (UINavigationController *)getNavigationController
//{
//    UINavigationController *currentNavViewController = [UIApplication getCurrentViewController].navigationController;
//
//    if (!currentNavViewController) {
//        currentNavViewController = [UIApplication sharedApplication].keyWindow.rootViewController ;
//    }
//
//    return currentNavViewController;
//}

- (LBNewCategoryMainView *)categoryView
{
    if (!_categoryView) {
        __weak __typeof(&*self)weakSelf = self;
        _categoryView = [[LBNewCategoryMainView alloc]initWithFrame:self.view.bounds isSingleSelected:self.isSingleSelected CodeArray:self.selectedCodeArray codeType:self.codeType maxSelectedLevel:self.maxSelectedLevel complete:^(NSArray<LBCategoryModel *> *modelArray, id parm) {
            if (weakSelf.clickBlock) {
                weakSelf.clickBlock(modelArray);
                [weakSelf.navigationController popToViewController:self animated:NO];
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
    return _categoryView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
